package input;

import force.CenterMass;
import force.GlobalForceApplier;
import force.Gravity;
import force.Viscosity;
import force.WallForce;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import sim.Simulation;

/**
 * A class to read in an environment data file, e.g.
 * environment.xsp.
 * 
 * @author Niel Lebeck and Wynne Lok
 *
 */
public class EnvironmentReader {
    /**
     * Read the environment data file.
     * @param sim associated Simulation
     * @param envFile file name
     */
    public void loadEnvironment(Simulation sim, File envFile) {
        try {
            Scanner input = new Scanner(envFile);
            while (input.hasNext()) {
                String lineString = input.nextLine();
                loadForce(lineString, sim);
            }
        }
        catch (FileNotFoundException e) {
            //do nothing, since the environment file is optional
            return;
        }
    }
    
    /**
     * Load a force from an input string.
     * @param str input string
     * @param sim Simulation
     */
    public void loadForce(String str, Simulation sim) {
        GlobalForceApplier gfa = sim.getGlobalForceApplier();
        Scanner s = new Scanner(str);
        String type = s.next();
        if ("gravity".equals(type)) {
            gfa.addGlobalForce(new Gravity(str));
        }
        else if ("viscosity".equals(type)) {
            gfa.addGlobalForce(new Viscosity(str));
        }
        else if ("centermass".equals(type)) {
            gfa.addGlobalForce(new CenterMass(str));
        }
        else if ("wall".equals(type)) {
            int id = s.nextInt();
            gfa.addGlobalForce(new WallForce(str));
        }
    }
}
